package com.nd.service;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.LogMapper;
import com.nd.pojo.bean.Log;
import com.nd.pojo.bean.LogExample;
import com.nd.pojo.bo.RoleBO;
import com.nd.pojo.vo.ErrorAndErrno;
import com.nd.pojo.vo.ListItems;
import com.nd.pojo.vo.LogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: project2mall
 * @description:
 * @author: shit_code_builder
 * @create: 2021-06-06 20:03
 **/
@Component
public class LogService {

    @Autowired
    LogMapper logMapper;


    public ErrorAndErrno<ListItems<LogVO>> selectListLog(RoleBO bo) {

        PageHelper.startPage(bo.getPage(),bo.getLimit());
        LogExample example = new LogExample();
        example.setOrderByClause(bo.getSort() + " " + bo.getOrder());
        LogExample.Criteria criteria = example.createCriteria();
        if (bo.getName()!=null||"".equals(bo.getName())) criteria.andAdminLike("%" + bo.getName() + "%");
        criteria.andDeletedEqualTo(false);
        List<Log> logs = logMapper.selectByExample(example);
        ArrayList<LogVO> logVOS = new ArrayList<>(logs.size());
        for (Log log : logs) {
            LogVO build = LogVO.builder().action(log.getAction()).comment(log.getComment()).id(log.getId()).ip(log.getIp()).updateTime(new DateTime(log.getUpdateTime()).toString())
                    .admin(log.getAdmin()).deleted(log.getDeleted()).result(log.getResult()).status(log.getStatus()).type(log.getType()).addTime(new DateTime(log.getAddTime()).toString()).build();
        logVOS.add(build);
        }
        PageInfo<Log> logMapperPageInfo = new PageInfo<>(logs);
        long total = logMapperPageInfo.getTotal();
        ListItems<LogVO> logListItems = new ListItems<>(logVOS);
        logListItems.setTotal(total);
        ErrorAndErrno<ListItems<LogVO>> listItemsErrorAndErrno = new ErrorAndErrno<>();
        listItemsErrorAndErrno.setData(logListItems);
        return listItemsErrorAndErrno;
    }
}
